package ks.client.gui.admin.user.listeners;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;


/**
 * UserModel for the batch create and batch delete tables.
 * 
 * @author Jennifer Hunt
 *
 */
public class UserModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	
	/** Elements for the model. */
	ArrayList<TempUser> elements;
	
	/**
	 * Constructor for UserModel.
	 */
	public UserModel () {
		this.elements = new ArrayList<TempUser>();
	}
	
	@Override
	public int getColumnCount() {
		return 2;
	}

	@Override
	public int getRowCount() {
		return elements.size();
	}

	/**
	 * Add TempUser object.
	 * 
	 * @param u
	 * @return
	 */
	public boolean add(TempUser u) {
		return elements.add(u);
	}
	
	/**
	 * Remove from the user model by actual TempUser object which is compared via equals
	 * method.
	 * 
	 * @param u
	 * @return
	 */
	public boolean remove(TempUser u) {
		return elements.remove(u);
	}
	
	/**
	 * Remove from the user model by user name.
	 * 
	 * @param name
	 * @return
	 */
	public boolean remove(String name) {
		for (int i = 0 ; i < elements.size(); i++) {
			TempUser u = elements.get(i);
			if (u.name.equals (name)) {
				return elements.remove(u);
			}
		}
		
		return false;
	}
	
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		TempUser u = elements.get(rowIndex);
		
		if (columnIndex == 0) {
			// name 
			return u.name;
		} else if (columnIndex == 1) {
			// password
			return u.password;
		} 
		
		// unknown
		return "";
	}

}
